package net.zjitc.config;


import net.zjitc.interceptor.AdminInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@EnableWebMvc
@Configuration
@ComponentScan("net.zjitc.controller")
public class SpringMvcConfig implements WebMvcConfigurer {
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AdminInterceptor()).addPathPatterns("/category/*").excludePathPatterns("/admin/login");
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        Logger logger = LoggerFactory.getLogger(SpringMvcConfig.class);
        logger.info("Configuring CORS mappings");

        registry.addMapping("/**") // 允许所有路径
                .allowedOrigins("http://localhost:8888") // 允许所有来源，也可以指定特定的域名，如 "http://example.com"
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法
                .allowedHeaders("*") // 允许的请求头
                .allowCredentials(true) // 是否允许发送Cookie
                .maxAge(3600); // 预检请求的有效期，单位为秒
    }
}
